﻿using FTBS.Models.Entities;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;

namespace FTBS.Models.DAO
{
    public class FilmDAO
    {
        private FilmTicketBookingSystemEntities _entities = new FilmTicketBookingSystemEntities();

        /// <summary>
        /// Load all film
        /// </summary>
        /// <returns>List of Film</returns>
        /// <author>VuongVT</author>
        public List<Film> LoadFilms()
        {
            try
            {
                List<Film> listFilm = new List<Film>();
                var films = from film in _entities.Films 
                            where film.IsDeleted == false 
                            orderby film.FormatId descending select film;
                if (films != null)
                {
                    listFilm = films.ToList();
                }
                return listFilm;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Load Film By Id
        /// </summary>
        /// <returns>Film</returns>
        /// <author>VuongVT</author>
        public Film LoadFilmsById(int id)
        {
            try
            {
                Film film = new Film();
                film = (from filmEnt in _entities.Films
                        where filmEnt.FilmId == id
                             && filmEnt.IsDeleted == false
                        select filmEnt).FirstOrDefault();
                return film;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Add a film
        /// </summary>
        /// <returns>1: Success -- 0: Fail</returns>
        /// <author>VuongVT</author>
        public int Add(Film film)
        {
            try
            {
                _entities.Films.Add(film);
                int resultQuery = _entities.SaveChanges();
                return resultQuery;
            }
            catch (Exception ex)
            {
                return 0;
                throw ex;
            }
        }

        /// <summary>
        /// Edit a Film
        /// </summary>
        /// <returns>1: Success -- 0: Fail</returns>
        /// <author>VuongVT</author>
        public int Edit(Film film)
        {
            try
            {
                _entities.Entry(film).State = EntityState.Modified;
                int resultQuery = _entities.SaveChanges();
                return resultQuery;
            }
            catch (Exception ex)
            {
                return 0;
                throw ex;
            }
        }

        /// <summary>
        /// Delete a film
        /// </summary>
        /// <returns>1: Success -- 0: Fail</returns>
        /// <author>VuongVT</author>
        public int Delete(Film film)
        {
            try
            {
                film.IsDeleted = true;
                _entities.Entry(film).State = EntityState.Modified;
                int result = _entities.SaveChanges();
                return result;
            }
            catch (Exception ex)
            {
                return 0;
                throw ex;
            }
        }
    }
}